package com.yonyou.cyxdms.sysAuth.service.controller.manage.company;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.ManageDictCodeConstants;
import com.yonyou.cyxdms.sysAuth.service.dto.CompanyDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.EquityDto;
import com.yonyou.cyxdms.sysAuth.service.dto.FileUploadInfoDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.dealer.ResponseDTO;
import com.yonyou.cyxdms.sysAuth.service.entity.CompanyPO;
import com.yonyou.cyxdms.sysAuth.service.mapper.CompanyMapper;
import com.yonyou.cyxdms.sysAuth.service.service.impl.EquityStructureService;
import com.yonyou.cyxdms.sysAuth.service.service.manage.company.CompanyService;
import com.yonyou.cyxdms.sysAuth.service.service.manage.company.FileUploadInfoService;
import com.yonyou.cyxdms.sysmanage.client.domains.dto.AscInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dealerCompany")
@Api(value = "公司信息维护", tags = {"公司信息维护"})
public class CompanyController {

    @Autowired
    CompanyService companyService;
    @Autowired
    EquityStructureService equityStructureService ;
    @Autowired
    FileUploadInfoService fileUploadInfoService;
    @GetMapping
    @SuppressWarnings("unchecked")
    @ApiOperation(value = "公司信息模块分页查询", notes = "公司信息模块分页查询", httpMethod = "GET",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public IPage<Map<String,Object>> getList (@RequestParam Map<String,Object> queryCondition, @RequestParam
            ("currentPage") int currentPage, @RequestParam("pageSize") int pageSize) {
//        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        if(queryCondition!=null){
            queryCondition.put("appId", ManageDictCodeConstants.APP_ID);
        }
        Page<Map<String,Object>> page = new Page(currentPage, pageSize);
        return companyService.selectPageBysql(page, queryCondition);
    }

    @GetMapping(value="/detail/{companyId}")
    @ApiOperation(value = "公司信息模块明细查询", notes = "公司信息模块明细查询", httpMethod = "GET",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public CompanyDTO getById(@PathVariable("companyId") Integer companyId) {
        return companyService.detail(companyId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "公司信息列表查询", notes = "公司信息列表查询", httpMethod = "GET")
    public List<CompanyDTO> getByParam(@RequestParam Map<String,Object> params){
        List<CompanyDTO> list = companyService.getListByParam(params);
        return list;
    }

    @PostMapping("")
    @ApiOperation(value = "公司信息模块新增", notes = "公司信息模块新增", httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public Map<String, Object> insert(@RequestBody CompanyDTO companyDTO){
        return companyService.insert(companyDTO);
    }

    @PutMapping("/{companyId}")
    @ApiOperation(value = "公司信息模块修改", notes = "公司信息模块修改", httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public Map<String, Object> update(@PathVariable("companyId") Integer companyId, @RequestBody CompanyDTO companyDTO){
        return companyService.update(companyId, companyDTO);
    }

    /**
     * 公司信息维护导出
     * @param companyDTO
     * @param request
     * @param response
     */
    @PostMapping(value = "/getListForExport")
    @ApiOperation(value = "公司信息维护导出", notes = "公司信息维护导出", httpMethod = "POST")
    public void getListForExport(@RequestBody CompanyDTO companyDTO, HttpServletRequest request, HttpServletResponse response) {
        companyService.getListForExport(companyDTO,request,response);
        //return ResultUtils.success();
    }

    /**
     * 客户主数据同步
     *
     * @param companyMap
     * @return
     */
    @PostMapping(value = "/getMainDataSync")
    @ApiOperation(value = "客户主数据同步", notes = "客户主数据同步", httpMethod = "POST")
    public Map<String,String> getMainDataSync(@RequestBody List<Map> companyMap) {
        return this.companyService.getMainDataSync(companyMap);
    }

    @GetMapping(value="/selectByCompanyId/{companyId}")
    @ApiOperation(value = "根据公司id查询股权信息", notes = "根据公司id查询股权信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "公司id", name = "companyId", required = true, dataType = "Integer", paramType = "header")})
    public List<EquityDto> selectByCompanyId(@PathVariable("companyId") Integer companyId) {
        return equityStructureService.selectByCompanyId(companyId);
    }
    @GetMapping(value="/selectFileByCompanyId/{companyId}")
    @ApiOperation(value = "根据公司id查询附件信息", notes = "根据公司id查询附件信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public List<FileUploadInfoDTO> selectFileByCompanyId(@PathVariable("companyId") Integer companyId ) {
        Integer billType = 17291001 ;
        return fileUploadInfoService.selectFileByCompanyId(companyId,billType);
    }
    @GetMapping(value="/selectUrlAndNameByCompanyId/{companyId}")
    @ApiOperation(value = "根据公司id查询附件信息URLandName", notes = "根据公司id查询附件信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string", paramType = "header")})
    public List<Map<String, String>> selectUrlAndNameByCompanyId(@PathVariable("companyId") Integer companyId ) {
        Integer billType = 17291001 ;
        return fileUploadInfoService.selectUrlAndNameByCompanyId(companyId,billType);
    }

   @GetMapping(value = "selectCode/{companyCode}")
   @ApiOperation(value = "根据公司代码,查询所有公司的代码和名称",notes = "根据公司代码查公司信息",httpMethod = "GET")
   @ApiImplicitParams({@ApiImplicitParam(value = "公司code" , name = "companyCode" , readOnly = true, dataType = "String" ,paramType = "header")})
    public  CompanyDTO selectCode(@PathVariable("companyCode") String companyCode ) {
        return this.companyService.selectCode(companyCode);
    }

    @GetMapping(value = "selectName/{companyName}/{companyCode}")
    @ApiOperation(value = "根据公司代码,查询所有公司的代码",notes = "根据公司代码查公司信息",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(value = "公司Name" , name = "companyName" , readOnly = true, dataType = "String" ,paramType = "header")})
    public CompanyDTO selectName(@PathVariable("companyName") String companyName ,@PathVariable("companyCode") String companyCode) {
        return this.companyService.selectName( companyName ,companyCode );
    }


}
